<?php
/**
 * created by zzy
 * date: 2018/2/26 11:45
 */


namespace App\Http\Admin\Order\Controllers;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\OrderProcess;
use App\Eloquent\Ygt\OrderProcessCourse;
use App\Engine\OrderEngine;
use Framework\BaseClass\Http\Admin\Controller;

class CourseController extends Controller
{
    public function price()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];
        $orderId            = request('oder_id',0);

//        $companyId          = 8;
//        $orderId            = 174;
        //返回对应工单涉及的员工id
        $where              = ['order_id'=>$orderId];
        $processIdArr       = OrderProcess::where($where)->get()->pluck('id');
        $courseCollection   = OrderProcessCourse::whereIn('order_process_id',$processIdArr)->get();
        $courseList         = $courseCollection->toArray();
        $userIdArr          = $courseCollection->pluck('uid')->toArray();
        $where              = [
            'company_id'=>$companyId,'user_id'=>['in',$userIdArr]
        ];
        $fields             = 'id,user_id,truename';
        $userCollection     = DepartmentUser::getList($where,$fields);
        $userList           = $userCollection->toArray();
        $userArr            = [];
        foreach ($userList as $key=>$val)
        {
            $userArr[$val['user_id']]   = $val['truename'];
        }
        foreach($courseList as $key=>$val)
        {
            $truename                        = $userArr[$val['uid']];
            $courseList[$key]['truename']   = $truename;
        }
        $courseListJson     = json_encode($courseList);
        $courseListJson     = str_replace('"', "'",$courseListJson);

        return $this->view('price',compact('courseListJson'));
    }
    public function setPrice()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];

        $requestJson        = $this->requestJson();
        $courseId           = $requestJson['id'] ? $requestJson['id'] : 0;
        $price              = $requestJson['price'] ? $requestJson['price'] : 0.00;
        //$courseId=223;
        if($courseId){
            $where          = ['id'=>$courseId];
            $data           = ['price'=>$price];
            $result         = OrderProcessCourse::updateOneData($where,$data);
            if($result){
                return $this->ajaxSuccess('成功');
            }else{
                return $this->ajaxFail('失败');
            }
        }else{
            return $this->ajaxFail('参数错误');
        }
    }
}